﻿using System.Linq;
using Microsoft.ApplicationServer.Http;

namespace ServiceWeb.Libs {

    public class ApiConfiguration : WebApiConfiguration {

        public ApiConfiguration() {
            EnableTestClient = true;

            RequestHandlers = (c, e, od) => {
                // TODO: Configure request operation handlers
                
            };

            this.AppendAuthorizationRequestHandlers();
        }
    }

    public static class ConfigExtensions {
        public static void AppendAuthorizationRequestHandlers(this WebApiConfiguration config) {
            var requestHandlers = config.RequestHandlers;
            config.RequestHandlers = (c, e, od) => {
                if (requestHandlers != null) {
                    requestHandlers(c, e, od); // Original request handler
                }
                var authorizeAttribute = od.Attributes.OfType<BasicHttpAuthorizationAttribute>().FirstOrDefault();
                if (authorizeAttribute != null) {
                    c.Add(new BasicHttpAuthorizationOperationHandler(authorizeAttribute));
                }
            };
        }
    }

}